package com.itheima.mybatis02.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import com.itheima.mybatis01.bean.Teacher;
import com.itheima.mybatis02.dao.TeacherMapper;
import com.itheima.mybatis02.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class TeacherServiceImpl implements TeacherService {
    @Override
    public List<Teacher> selectAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        //PageHelper.startPage(1,3);
        //PageHelper.startPage(2,2);
        PageHelper.startPage(3, 2);
        List<Teacher> list = mapper.selectAll();
        sqlSession.close();
        return list;
    }

    @Override
    public Teacher selectOne(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.selectOne(id);
        sqlSession.close();
        return teacher;
    }

    @Override
    public int insert(Teacher teacher) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        int insert = mapper.insert(teacher);
        sqlSession.close();
        return insert;
    }

    @Override
    public int update(Teacher teacher) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        int update = mapper.update(teacher);
        sqlSession.close();
        return update;
    }

    @Override
    public int delete(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        int delete = mapper.delete(id);
        sqlSession.close();
        return delete;
    }

    @Test
    public void selectByNameAndAge() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher1 = mapper.findByTeacherNameAndAge("王五", 25);
        System.out.println(teacher1);
    }

    @Test
    public void selectCondition() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = new Teacher();
        teacher.setName("王五");
        Teacher teacher1 = mapper.selectCondition(teacher);
        System.out.println(teacher1);
    }

    @Test
    public void selectCondition2() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = new Teacher();
        teacher.setAge(21);
        Teacher teacher1 = mapper.selectCondition2(teacher);
        System.out.println(teacher1);
    }

    @Test
    public void selectById() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        List<Teacher> list1 = mapper.selectById(list);
        for (Teacher teacher : list1) {
            System.out.println(teacher);
        }
    }
    @Test
    public void selectById1(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);
        PageHelper.startPage(2,3);
        List<Teacher> list1 = mapper.selectById1(list);
        for (Teacher teacher : list1) {
            System.out.println(teacher);
        }
    }
}

